// pages/bindPhone/bindPhone.js
var common = require("../../utils/Conmon.js");

var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    firstStep: true,
    sencodnStep: false,
    focus: true,
    Phone: '',
    NewPhone: '',
    code:''  //验证码
  },
  /**
   * 输入新手机号
   */
  bindPhoneClickTap:function(event){
    this.setData({
      Phone: event.detail.value 
    });
  },

  /**
   * 输入手机号后点击下一步发送验证码
   */
  nextStep: function () {
    var that=this;
    var _phone = that.data.Phone;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if(_phone.length==0||_phone=='')
    {
       wx.showModal({ title: '警告', content: '请输入手机号', showCancel: false })
       return;
    }
    if (!myreg.test(_phone))
    {
      wx.showModal({ title: '警告', content: '号码格式错误', showCancel: false })
      return;
    }
    wx.showLoading({ title: '提交中...', mask: true }); //发送获取验证码并验证手机号是否已绑定
    that.wxSubRequest('GET', appData.apiurl + 'member/api/MemberCenter/SmsMsg', { custId: appData.custID, phone: _phone },
      function (data) {
        if (data.content.status == 1200) {
            wx.hideLoading()
            that.setData({
              firstStep: true,
              sencodnStep: false,
              NewPhone: _phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
            });
            wx.showModal({ title: '提示', content: '现验证码:6666', showCancel: false })
        }
        else{
          wx.hideLoading();
          wx.showModal({ title: '错误', content: data.content.msg, showCancel: false })
        }
      },
      function (obj) {
        wx.hideLoading()
        console.log(obj)
      })
  },

  /**
   * 输入手机号获得焦点事件
   */
  focused: function () {
    this.setData({
      focus: false
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var val = options.phone;
    var step1=true;
    var step2=false;
    if(val==undefined||val==null||val=='')
    {
      step1=false;
      step2=true;
    }else{
       val=val.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
       wx.showModal({ title: '提示', content: '现验证码:6666', showCancel: false })
    }
    that.setData({
      NewPhone: val,
      firstStep: step1,
      sencodnStep: step2
    })
  },
  
  /**
   * 输入验证码
   */
  bindCodeClickTap:function(event){
     this.setData({
       code: event.detail.value
     })
  },

 /**
  * 获取验证码后点击下一步
  */
  bindClickTap:function(event){
    var that=this;
    var code = that.data.code;
    if (code.length==0||code=='')
    {
      wx.showModal({ title: '警告', content: '请输入验证码', showCancel: false })
      return;
    }else{
      if (code!='6666'){
        wx.showModal({ title: '警告', content: '验证码不正确', showCancel: false })
        return;
      }
    }
    //wx.showLoading({ title: '提交中...', mask: true }); //提交验证 验证码
    if (that.data.Phone.length == 0 || that.data.Phone=='')
    {
      that.setData({
        firstStep: false,
        sencodnStep: true,
        code: ''
      });
    }else{
      var param = { Phone: this.data.Phone, UserID: wx.getStorageSync('UserInfo').UserID, CustID: appData.custID, Yzm: code };
      wx.showLoading({ title: '提交中...', mask: true }); //发送获取验证码并验证手机号是否已绑定
      that.wxSubRequest('POST', appData.apiurl+'member/api/MemberCenter/UpdatePhone', param,
        function (data) {
          if (data.code == 1200) {
            wx.hideLoading()
            that.setData({
              code: '',
              Phone: ''
            });
            wx.redirectTo({
              url: '/pages/personalData/personalData',
            })
          }
          else {
            wx.hideLoading()
            wx.showModal({ title: '错误', content: '发生错误', showCancel: false })
          }
        },
        function (obj) {
          wx.hideLoading()
          console.log(obj)
        })
      
    }
  },

  
  /**
 * 微信提交请求获取数据或保存数据
 * 
 */
  wxSubRequest: function (method, url, param, callBack, CallBackError) {
    wx.request({
      url: url,
      method: method,
      data: param,
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName

      },
      success: function (res) {
        if (res.statusCode == 200) {
          var data = res.data;
          callBack(data)
          console.log(res.data)
        }
      },
      fail: function (res) {
        CallBackError(res.data)
        console.log(res.data)
      },
      complete: function (res) {
        console.log(res.msg)
      }
    })
  },

/***************************************************** */
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})